package org.jeecg.modules.job.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.job.rms.entity.RmsCompany;
import org.jeecg.modules.job.rms.mapper.RmsCompanyMapper;
import org.jeecg.modules.job.rms.mapper.RmsCompanyPositionMapper;
import org.jeecg.modules.job.rms.mapper.RmsCourseMapper;
import org.jeecg.modules.job.rms.mapper.RmsOuttaskMapper;
import org.jeecg.modules.job.ums.entity.UmsShareLog;
import org.jeecg.modules.job.ums.mapper.UmsShareLogMapper;
import org.jeecg.modules.job.ums.service.IUmsMemberRoleService;
import org.jeecg.modules.job.ums.service.IUmsShareLogService;
import org.jeecg.modules.job.wms.mapper.WmsResumeMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * @Description: 分享记录
 * @Author: jeecg-boot
 * @Date:   2022-12-18
 * @Version: V1.0
 */
@Service
public class UmsShareLogServiceImpl extends ServiceImpl<UmsShareLogMapper, UmsShareLog> implements IUmsShareLogService {


    @Resource
    private RmsCompanyPositionMapper companyPositionMapper;
    @Resource
    private WmsResumeMapper resumeMapper;
    @Resource
    private RmsCompanyMapper companyMapper;
    @Resource
    private RmsCourseMapper courseMapper;
    @Resource
    private RmsOuttaskMapper outtaskMapper;
    @Resource
    private IUmsMemberRoleService memberRoleService;
    @Resource
    private ISysBaseAPI sysBaseAPI;


    @Override
    public boolean addOrUpdate(UmsShareLog param) {
        UmsShareLog result=this.getOne(new QueryWrapper<>(param));
        if (result!=null){
            param.setId(result.getId());
            return this.updateById(param);
        }else{
            return this.save(param);
        }
    }

    @Override
    public IPage<UmsShareLog> findPageBySpec(Page<UmsShareLog> page, UmsShareLog param) {
        IPage<UmsShareLog> pageInfo = this.baseMapper.selectPage(page, new QueryWrapper<>(param));
        page.getRecords().forEach(item -> {
//            类型：0-职位分享，1-课程分享，3-外包分享
            if (item.getShareType()==0){
                item.setPosition(companyPositionMapper.selectById(item.getDataId()));
                //招聘者信息
                LoginUser memberResult=sysBaseAPI.getUserById(item.getComMemberId());
                item.setMemberAvatar(memberResult.getAvatar());
                item.setMemberName(memberResult.getRealname());
                item.setMemberPostName(memberRoleService.getMemberRole(memberResult.getId()).getPostName());
                //企业信息
                RmsCompany company=companyMapper.selectById(item.getCompanyId());
                if (company!=null){
                    item.setCompanyName(company.getName());
                    item.setCompanyStaffSize(company.getStaffSize());
                    item.setCompanyNature(company.getCompanyNature());
                }

            }else if(item.getShareType()==1){//课程分享
                item.setCourse(courseMapper.selectById(item.getDataId()));
            }else if(item.getShareType()==2){//外包分享
                item.setTask(outtaskMapper.selectById(item.getDataId()));
            }
        });
        return pageInfo;
    }
}
